library(foreign)
library(memisc)
library(psych)
library(lavaan)
library(car)
library(stats)
library(xtable)
library(MASS)
library(stargazer)
library(e1071)
library(Hmisc)
library(lme4)
library(effects)
library(lattice)
library(sjPlot)
library(interplot)
library(scales)
library(visreg)
library(lmerTest)

data <- read.csv(".../Diss_NSF_long_combined.csv")

table(data$race)
data$race <- as.factor(data$race)
data$race <- relevel(data$race, "White")
data$noncon <- as.factor(data$noncon)
data$con <- as.factor(data$con)
data$con <- relevel(data$con, "Nonconscious")


attach(data)


#########Empty Model (trial and participants, and statement and specific image)#####
table(support)
lmer1 <- lmer(support ~ 1 + (1|participant) + (1|trial) + (1|prime1) + (1|block))
summary(lmer1) #about .38% for trial, .04$ for prime1, and 9.88% for participant
pro1 <- profile(lmer1)
confint(pro1)
xyplot(pro1, aspect=1.3) 
ranef(lmer1) #intercepts by individual participant
dotplot(ranef(lmer1, condVar=T)) #cool
qqmath(ranef(lmer1, condVar=T)) 


#####MAIN ANALYSES#####

#####****Race and Noncon#####

lmer1 <- lmer(support ~ noncon + race + (1|participant))
summary(lmer1)
visreg(lmer1, "race")

lmer1 <- lmer(support ~ noncon + race + male + nonChristian + education + saw_race + 
                (1|participant))
summary(lmer1) 

lmer1 <- lmer(support ~ con*race + male + nonChristian + education + saw_race + 
                (1|participant))
summary(lmer1) 
visreg(lmer1, "race", "con")


#####SUPPLEMENTARY ANALYSES#####

#####****RB policy support#####


lmer1 <- lmer(support ~ BlAAc*race + noncon + male + nonChristian + education + saw_race + (1|participant))
summary(lmer1)
visreg(lmer1, "race", "BlAAc")

#####****conscious racial bias inhibition (CRBI)#####

cor.test(data$racial_bias_con_noncon, data$BlAAc, na.rm=T)

summary(data$racial_bias)
summary(data$racial_bias_con)
summary(data$racial_bias_noncon)

table(data$participant)

summary(data$racial_bias_con_noncon)
sd(data$racial_bias_con_noncon, na.rm=T)
hist(data$racial_bias_con_noncon)

table(data$racial_bias_con_noncon, data$participant)

data %>%
  group_by(participant) %>%
  summarise(avg = mean(racial_bias_con_noncon)) %>%
  print(n=25)

whitesupport <- rowMeans(data[,c(colnames(data[grep("support_white", 
                                                    names(data))]))], na.rm=T)

blacksupport <- rowMeans(data[,c(colnames(data[grep("support_black", 
                                                    names(data))]))], na.rm=T)

summary(whitesupport)
sd(whitesupport, na.rm=T)
summary(blacksupport)
sd(blacksupport, na.rm=T)




